<template>
  <!-- 我的商户 -->
  <div class="ContactPerson">
    <div class="searchNav">
      <!-- <h2>联系人</h2> -->
      <div class="searchParams">
        <div style="">
          <el-input
            placeholder="请选择商家"
            v-model="selectCustomer"
            class="input-with-select"
            disabled
            style="width: 300px; margin-left: 10px"
          >
            <el-button
              style="margin-left: 8%"
              slot="suffix"
              icon="el-icon-search"
              type="primary"
              @click="handleAddCustomer()"
            ></el-button>
          </el-input>
        </div>
        <el-input
          class="input"
          v-model="queryParam.BuerNamorPhonee"
          placeholder="请输入商户/联系方式"
          clearable
          @keydown.enter.native="search"
        ></el-input>
        <el-button class="button" @click="search" type="primary"
          >查询</el-button
        >
        <el-button class="button" @click="reset('reset')">重置</el-button>

        <!--        { MerchantId:"1701482713493540864"}-->
        <!--     后续需要打开   <el-button class="button" @click="handleMyMerchantDetail(childrenRowObj)" type="primary">查看资质</el-button>-->
      </div>
      <el-button
        class="button"
        icon="el-icon-plus"
        type="primary"
        style="margin-left: 0"
        @click="handleAdd"
        >添加贷款商户</el-button
      >
    </div>
    <!-- 表格 -->
    <div>
      <el-table :data="tableData" style="width: 100%" :max-height="height">
        <el-table-column prop="CompanyId" label="编码"> </el-table-column>
        <el-table-column prop="Title" label="名称"></el-table-column>
        <el-table-column prop="CreateTime" label="注册日期">
          <template slot-scope="scope">
            <span>{{
              scope.row.CreateTime ? scope.row.CreateTime.substring(0, 19) : ""
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="UserPhone" label="联系方式"> </el-table-column>
        <el-table-column prop="Address" label="地址"> </el-table-column>
        <el-table-column prop="Address" label="授信额度 / 已用额度">
          <template slot-scope="scope">
            <span>{{
              toThousands(scope.row.Totalamount) +
              " / " +
              toThousands(scope.row.Usedamount)
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleMyMerchantDetail(scope.row)"
              >资质详情</el-button
            >
            <el-button type="text" @click="handleLimit(scope.row)"
              >设置额度</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div style="display: flex; justify-content: flex-end; margin-top: 8px">
        <pagination
          :total="pagination.total"
          :page.sync="pagination.PageIndex"
          :limit.sync="pagination.PageRows"
          @pagination="handlePageChange"
        />
      </div>
    </div>
    <!--设置额度-->
    <el-dialog
      title="设置额度"
      :visible.sync="limitMoneyObj.dialog"
      width="30%"
    >
      <el-input
        v-model="limitMoneyObj.value"
        placeholder="请输入额度"
        type="number"
      ></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="limitMoneyObj.dialog = false">取 消</el-button>
        <el-button type="primary" @click="handlerLimitConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增商户 -->
    <EditForm ref="editForm" :parentObj="{ ...this }" />

    <!-- 资质详情 -->
    <MyMerchantDetail ref="MyMerchantDetail" :parentObj="{ ...this }" />

    <!-- 选择商户-->
    <ConsigneeInquiry
      ref="consigneeInquiry"
      @handlerConfirm="handlerConfirm"
      :parentObj="{ ...this }"
    />
  </div>
</template>

<script>
import radioGroup from "@/components/CompanyRadioGroup.vue";
import { toThousands } from "@/utils/Tools";
import EditForm from "./MyMerchantForm.vue";
import MyMerchantDetail from "./MyMerchantDetail.vue";
//选择商户
import ConsigneeInquiry from "@/components/ConsigneeInquiry.vue";
export default {
  props: {},
  components: {
    EditForm,
    MyMerchantDetail,
    radioGroup,
    ConsigneeInquiry,
  },
  mounted() {
    let userInfo = JSON.parse(sessionStorage.getItem("newUserInfo"));
    this.userInfo = userInfo;
    // this.queryParam.Keys = userInfo.Id
    console.log("获取缓存信息==", this.userInfo);

    //获取用户信息
    this.getUserInfo();
    // 获取商户列表数据 默认选择第一个
    this.getDataList();
  },
  created() {},
  computed: {
    height() {
      return document.documentElement.clientHeight * 0.75;
    },
  },
  data() {
    return {
      // 设置额度
      limitMoneyObj: {
        // 打开关闭弹窗
        dialog: false,
        // 输入的额度
        value: "",
        // 选中的当前行
        row: {},
      },
      // 选择好的商家
      selectCustomer: "",
      // 整体页面加载
      fullscreenLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        pageSizeOptions: [10, 20, 30, 50],
      },
      sorter: {
        field: "Id",
        order: "desc",
      },

      tableData: [],
      range: [],
      queryParam: {
        // 输入的查询条件
        BuerNamorPhonee: "",
        Condition: "",
      },
      loading: false,
      toThousands,
      userInfo: {},
      // 默认第一家商户的对象
      childrenRowObj: {},
      //   默认首次加载第一条商户的数据
      firstTableListData: [],
    };
  },
  methods: {
    // 选择客户弹窗
    handleAddCustomer() {
      //初始化清空输入框内容
      // console.log("this.userInfo.UserInfo.Id", this.userInfo.Id);
      this.$refs.consigneeInquiry.queryParam.Keyword = "";
      this.$refs.consigneeInquiry.queryParam.Condition = this.userInfo.Id;
      this.$refs.consigneeInquiry.openForm();
    },
    // 选择商户确定
    handlerConfirm(data) {
      var that = this;
      console.log("子组件传递过来的参数", data);
      this.firstTableListData = [data];
      this.childrenRowObj = data;
      //   赋值选择好的商户
      this.selectCustomer = data.Title;
      //   更新当前选择好的商户列表数据
      this.getCustomerListData(data.Id);
    },
    // 设置额度
    handleLimit(row) {
      this.limitMoneyObj.row = row;
      console.log("设置额度", row);
      this.limitMoneyObj.value = row.Totalamount;
      // 设置额度弹窗
      this.limitMoneyObj.dialog = true;
    },

    // 设置额度确定
    handlerLimitConfirm() {
      if (this.limitMoneyObj.value === "") {
        this.$message.error("请设置额度");
        return;
      }
      console.log("继续走了", this.limitMoneyObj);
      // 关闭设置额度弹窗
      this.limitMoneyObj.dialog = false;
      let obj = {
        Totalamount: this.limitMoneyObj.value,
        StoreId: this.limitMoneyObj.row.StoreId,
        BankId: this.limitMoneyObj.row.BankId,
        CreatorId: this.limitMoneyObj.row.CreatorId,
      };
      // 提交设置额度
      this.$http.post("/Base/Base_Quota/SetQuota", obj).then((res) => {
        if (res.Success) {
          this.$message.success("设置成功");
          // 关闭弹窗
          this.limitMoneyObj.dialog = false;
          // 重新获取我的商户列表
          this.getCustomerListData(this.childrenRowObj.Id);
        } else {
          this.limitMoneyObj.dialog = true;
          this.$message.error(res.Msg);
        }
      });
    },
    // 获取用户信息
    getUserInfo() {
      this.$http.post("/Base_Manage/Home/GetBankOperatorInfo").then((res) => {
        if (res.Success) {
          // 测试存储  待修改新登录
          sessionStorage.setItem("newUserInfo", JSON.stringify(res.Data));
          this.userInfo = res.Data.UserInfo;
          console.log("获取用户信息===========", res);
          // this.getDataList();
        }
      });
    },
    // radioComplete(e) {
    //   if (e) {
    //     this.queryParam.SupplieId = e;
    //     this.getDataList();
    //   }
    // },
    // radioChange(e) {
    //   console.log(e);
    //   if (e == "more") return;
    //   this.queryParam.SupplieId = e;
    //   this.getDataList();
    // },
    // 开始结束时间选择
    changeTime(e) {
      // this.range = e;
      // this.queryParam.StartTime = e[0];
      // this.queryParam.EndTime = e[1];
      // console.log("开始时间", e, this.queryParam);
      // this.getDataList();
    },
    // 新增我的商户
    handleAdd() {
      console.log("新增客户", this.queryParam);
      this.$refs.editForm.openForm();

      return;
      if (this.queryParam.SupplieId.split(",").length > 1) {
        this.$refs.editForm.openForm();
      } else {
        this.$refs.editForm.openForm(null, this.queryParam.SupplieId);
      }
      // this.$refs.editForm.openForm(null);
    },
    // 参数重置
    reset(event) {
      if (event === "reset") {
        this.queryParam.BuerNamorPhonee = "";
      }
      this.tableData = [];
      this.pagination.current = 1;
      this.getDataList();
    },
    // 搜索
    search() {
      this.getCustomerListData(this.childrenRowObj.Id);
    },
    //资质详情
    handleMyMerchantDetail(row) {
      console.log("资质详情", row);
      this.$refs.MyMerchantDetail.openForm(row.CompanyId);
      // if(row.MerchantId){
      //   // 如果有这个id说明是从当前最外层的列表单条点击进去的  否则就是从上面的查看资质进去的 传不同的id
      //   this.$refs.MyMerchantDetail.openForm(row.MerchantId)
      // }else{
      //   if(this.selectCustomer === "") {
      //     this.$message.error('请先选择商户')
      //   }else{
      //     console.log('是否优质选择的商户', this.selectCustomer)
      //     this.$refs.MyMerchantDetail.openForm(row.Id);
      //   }
      // }
    },
    // 分页函数
    handlePageChange(data) {
      this.pagination.current = data.pageIndex;
      this.pagination.pageSize = data.pageSize;
      this.getCustomerListData(this.childrenRowObj.Id);
    },
    // 获取选择的商户列表第一条数据
    getDataList() {
      var that = this;
      console.log(
        "获取用户信息了",
        JSON.parse(sessionStorage.getItem("newUserInfo"))
      );
      let userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
      console.log("获取银行iduserInfouserInfouserInfo", userInfo);
      this.queryParam.Condition = userInfo.Id;
      this.$http
        .post("/Base_Manage/Base_Company/GetBankBusiness", {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || "Id",
          SortType: this.sorter.order,
          Search: this.queryParam,
        })
        .then((resJson) => {
          this.loading = false;
          if (resJson.Success) {
            //   说明第一次进来  渲染第一条数据
            if (resJson.Data.length !== 0) {
              this.selectCustomer = resJson.Data[0].Title;
              this.childrenRowObj = resJson.Data[0];
              this.getCustomerListData(this.childrenRowObj.Id);
            }
            console.log("获取第一家商户列表", this.childrenRowObj);
            if (this.tableData.length > 0) {
              const pagination = { ...this.pagination };
              pagination.total = resJson.Total;
              this.pagination = pagination;
            }
          }
        });
      // //创建loading对象开始遮罩
      // this.loading = true;
      // this.$http
      //   .post("/Base/Bank_InfoClient/GetBankDetails", {
      //     PageIndex: this.pagination.current,
      //     PageRows: this.pagination.pageSize,
      //     SortField: this.sorter.field || "Id",
      //     SortType: this.sorter.order,
      //     Search: {
      //       Keys: this.userInfo.Id,
      //       keyword:this.queryParam.keyword
      //     },
      //   })
      //   .then((resJson) => {
      //     //请求结束关闭loading
      //     setTimeout(function () {
      //       that.loading = false;
      //     }, 600);
      //
      //     if (resJson.Success) {
      //       console.log('获取我的商户列表数据', resJson)
      //       this.tableData = resJson.Data;
      //       if (this.tableData) {
      //         const pagination = { ...this.pagination };
      //         pagination.total = resJson.Total;
      //         this.pagination = pagination;
      //       }
      //     }
      //   });
    },
    //   获取我的商户所有列表
    getCustomerListData(StoreId) {
      this.$http
        .post("/Base_Manage/Base_Company/GetBuyerBusiness", {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || "Id",
          SortType: this.sorter.order,
          Search: {
            BankId: this.userInfo.Id,
            StoreId: StoreId,
            BuerNamorPhonee: this.queryParam.BuerNamorPhonee,
          },
        })
        .then((resJson) => {
          this.loading = false;
          if (resJson.Success) {
            this.pagination.total = resJson.Total;
            //   说明第一次进来  渲染第一条数据
            this.tableData = resJson.Data;
            if (this.tableData.length > 0) {
              const pagination = { ...this.pagination };
              pagination.total = resJson.Total;
              this.pagination = pagination;
            }
          }
        });
    },
  },
  watch: {},
};
</script>

<style lang="less" scoped></style>
